<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="welcome" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Spring IOC原理 |  ChenyyのBlog
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  
<link rel="stylesheet" href="/css/custom.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Java/Spring IOC原理"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Spring IOC原理
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/05/03/Java/Spring%20IOC%E5%8E%9F%E7%90%86/" class="article-date">
  <time datetime="2021-05-03T02:21:58.000Z" itemprop="datePublished">2021-05-03</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java%E6%A1%86%E6%9E%B6/">Java框架</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">8 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <ul>
<li>回顾以前对象依赖</li>
<li>Spring依赖注入</li>
<li>IOC控制反转</li>
</ul>
<span id="more"></span>

<h1 id="回顾以前对象依赖"><a href="#回顾以前对象依赖" class="headerlink" title="回顾以前对象依赖"></a>回顾以前对象依赖</h1><p>传统的实现，需要自己new对象，然后给对象属性赋值，一旦有改动，特别麻烦。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">class  UserService&#123;</span><br><span class="line">    UserDao userDao = new UserDao();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>后来，使用DaoFactory，通过字符串创建对应的对象。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    private UserDao userDao = DaoFactory.getInstance().createDao(&quot;zhongfucheng.dao.impl.UserDaoImpl&quot;, UserDao.class);</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">public class DaoFactory &#123;</span><br><span class="line"></span><br><span class="line">    private static final DaoFactory factory = new DaoFactory();</span><br><span class="line">    private DaoFactory()&#123;&#125;</span><br><span class="line"></span><br><span class="line">    public static DaoFactory getInstance()&#123;</span><br><span class="line">        return factory;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public &lt;T&gt; T createDao(String className,Class&lt;T&gt; clazz)&#123;</span><br><span class="line">        try&#123;</span><br><span class="line">            T t = (T) Class.forName(className).newInstance();</span><br><span class="line">            return t;</span><br><span class="line">        &#125;catch (Exception e) &#123;</span><br><span class="line">            throw new RuntimeException(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>再后来，我们在DaoFactory中读取配置文件，根据配置文件来创建对象</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    UserDao dao = DaoFactory.getInstance().createUserDao();</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">public class DaoFactory &#123;</span><br><span class="line"></span><br><span class="line">    private  UserDao userdao = null;</span><br><span class="line"></span><br><span class="line">    private DaoFactory()&#123;</span><br><span class="line">        try&#123;</span><br><span class="line">            InputStream in = DaoFactory.class.getClassLoader().getResourceAsStream(&quot;dao.properties&quot;);</span><br><span class="line">            Properties prop = new Properties();</span><br><span class="line">            prop.load(in);</span><br><span class="line"></span><br><span class="line">            String daoClassName = prop.getProperty(&quot;userdao&quot;);</span><br><span class="line">            userdao = (UserDao)Class.forName(daoClassName).newInstance();</span><br><span class="line"></span><br><span class="line">        &#125;catch (Exception e) &#123;</span><br><span class="line">            throw new RuntimeException(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private static final DaoFactory instance = new DaoFactory();</span><br><span class="line"></span><br><span class="line">    public static DaoFactory getInstance()&#123;</span><br><span class="line">        return instance;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    public UserDao createUserDao()&#123;</span><br><span class="line">        return userdao;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503160931.png"></p>
<h1 id="Spring依赖注入"><a href="#Spring依赖注入" class="headerlink" title="Spring依赖注入"></a>Spring依赖注入</h1><p>对象之间的依赖关系，其实就是给对象上的属性赋值！因为对象上有其他对象的变量，因此存在了依赖。</p>
<p>Spring提供了好几种的方式来给属性赋值</p>
<ul>
<li><ol>
<li>构造函数注入，实现强制依赖。</li>
</ol>
</li>
<li><ol start="2">
<li>属性注入setter()方法，实现可选依赖。</li>
</ol>
</li>
<li><ol start="3">
<li>自动装配（xml/注解）。</li>
</ol>
</li>
<li><ol start="4">
<li>工厂方法接口注入，遗留方法。</li>
</ol>
</li>
</ul>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503164125.png"></p>
<h2 id="构造函数给属性赋值"><a href="#构造函数给属性赋值" class="headerlink" title="构造函数给属性赋值"></a>构造函数给属性赋值</h2><p>在serice中加入一个构造函数，参数就是userDao</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public class UserService &#123;</span><br><span class="line"></span><br><span class="line">    private UserDao userDao; </span><br><span class="line"></span><br><span class="line">    public UserService(UserDao userDao) &#123;</span><br><span class="line">        this.userDao = userDao;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>applicationContext.xml配置文件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--创建userDao对象--&gt;</span><br><span class="line">&lt;bean id=&quot;userDao&quot; class=&quot;UserDao&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--创建userService对象--&gt;</span><br><span class="line">&lt;bean id=&quot;userService&quot; class=&quot;UserService&quot;&gt;</span><br><span class="line">    &lt;!--要想在userService层中能够引用到userDao，就必须先创建userDao对象--&gt;</span><br><span class="line">    &lt;constructor-arg index=&quot;0&quot; name=&quot;userDao&quot; type=&quot;UserDao&quot; ref=&quot;userDao&quot;&gt;&lt;/constructor-arg&gt;</span><br><span class="line">&lt;/bean&gt;</span><br></pre></td></tr></table></figure>



<h2 id="通过set方法给属性注入值"><a href="#通过set方法给属性注入值" class="headerlink" title="通过set方法给属性注入值"></a>通过set方法给属性注入值</h2><p>在service层通过set方法来把userDao注入到UserService中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class UserService &#123;</span><br><span class="line"></span><br><span class="line">    private UserDao userDao; </span><br><span class="line"></span><br><span class="line">    public void setUserDao(String userId,String userName) &#123;</span><br><span class="line">    	UserDao userDao = new UserDao();</span><br><span class="line">    	userDao.setId(userId);</span><br><span class="line">    	userDao.setName(userName);</span><br><span class="line">        this.userDao = userDao;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>applicationContext.xml配置文件：通过property节点来给属性赋值，引用类型使用ref属性，基本类型使用value属性。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--创建userDao对象--&gt;</span><br><span class="line">&lt;bean id=&quot;userDao&quot; class=&quot;UserDao&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--创建userService对象--&gt;</span><br><span class="line">&lt;bean id=&quot;userService&quot; class=&quot;UserService&quot;&gt;</span><br><span class="line">    &lt;property name=&quot;userDao&quot; ref=&quot;userDao&quot;/&gt;</span><br><span class="line">&lt;/bean&gt;</span><br></pre></td></tr></table></figure>



<p><strong>内部Bean</strong></p>
<p>先创建userService，发现userService需要userDao的属性，再创建userDao。缺点是如果Bean要多次被使用的话，就要多次配置。</p>
<p>applicationContext.xml配置文件：property节点内置bean节点</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--</span><br><span class="line">    1.创建userService，看到有userDao这个属性</span><br><span class="line">    2.而userDao这个属性又是一个对象</span><br><span class="line">    3.在property属性下又内置了一个bean</span><br><span class="line">    4.创建userDao</span><br><span class="line">--&gt;</span><br><span class="line">&lt;bean id=&quot;userService&quot; class=&quot;UserService&quot;&gt;</span><br><span class="line">    &lt;property name=&quot;userDao&quot;&gt;</span><br><span class="line">        &lt;bean id=&quot;userDao&quot; class=&quot;UserDao&quot;/&gt;</span><br><span class="line">    &lt;/property&gt;</span><br><span class="line">&lt;/bean&gt;</span><br></pre></td></tr></table></figure>



<p><strong>p 名称空间注入属性值</strong></p>
<p><strong>applicationContext.xml配置文件：使用p名称空间</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;bean id=&quot;userDao&quot; class=&quot;UserDao&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--不用写property节点了，直接使用p名称空间--&gt;</span><br><span class="line">&lt;bean id=&quot;userService&quot; class=&quot;UserService&quot; p:userDao-ref=&quot;userDao&quot;/&gt;</span><br></pre></td></tr></table></figure>



<h2 id="自动装配"><a href="#自动装配" class="headerlink" title="自动装配"></a>自动装配</h2><p>自动装载默认是不打开的，修改applicationContext.xml配置文件使其生效。</p>
<p>byName：通过参数名自动装配，装配和该bean的属性具有相同名字的bean。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;bean id=&quot;userDao&quot; class=&quot;UserDao&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--</span><br><span class="line">    1.通过名字来自动装配</span><br><span class="line">    2.发现userService中有个叫userDao的属性</span><br><span class="line">    3.看看IOC容器中没有叫userDao的对象</span><br><span class="line">    4.如果有，就装配进去</span><br><span class="line">--&gt;</span><br><span class="line">&lt;bean id=&quot;userService&quot; class=&quot;UserService&quot; autowire=&quot;byName&quot;/&gt;</span><br></pre></td></tr></table></figure>

<p>byType：通过参数类型自动装配，装配和该bean的属性具有相同类型的bean。如果有多个bean符合条件，则抛出错误。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;bean id=&quot;userDao&quot; class=&quot;UserDao&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--</span><br><span class="line">    1.通过名字来自动装配</span><br><span class="line">    2.发现userService中有个叫userDao的属性</span><br><span class="line">    3.看看IOC容器UserDao类型的对象</span><br><span class="line">    4.如果有，就装配进去</span><br><span class="line">--&gt;</span><br><span class="line">&lt;bean id=&quot;userService&quot; class=&quot;UserService&quot; autowire=&quot;byType&quot;/&gt;</span><br></pre></td></tr></table></figure>



<p>@Resource默认通过反射按名称装配，找不到才按类型装配。<br>使用name属性指定byName策略，使用type属性指定byType策略。</p>
<p>@Autowired注解来实现自动装配。<br>默认按类型装配。结合@Qualifier(“name”)按照名称装配。required属性为false时允许null值。</p>
<ul>
<li>写在属性上，先根据属性的类型（byType）去Spring容器找对应类型的bean，找到多个再根据属性的名字确定，再通过反射的方式赋值给这个属性。</li>
<li>写在setter方法上，会根据setter方法的参数类型去找bean，<br>找到多个再根据属性的名字筛选，再调用set方法进行赋值。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">public class UserService &#123;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private UserDao userDao ;</span><br><span class="line"></span><br><span class="line">//    @Autowired</span><br><span class="line">//    public void setUserDao(UserDao userDao) &#123;</span><br><span class="line">//        this.userDao = userDao;</span><br><span class="line">//    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="IOC控制反转"><a href="#IOC控制反转" class="headerlink" title="IOC控制反转"></a>IOC控制反转</h1><p>**Spring使用依赖注入来实现对象之间的依赖关系 ** DI dependency injection。</p>
<p><strong>Spring将对象的创建交给外部容器完成称为控制反转</strong> IOC  Inversion of Control。</p>
<p>这个<strong>外部容器就是IOC容器</strong>。</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503173616.png"></p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503173756.png"></p>
<p>使用IOC的优点：</p>
<ol>
<li>实现代码之间的解耦，提高程序的灵活性和可维护性。</li>
<li>IOC容器支持加载服务时的饿汉式初始化和懒加载。</li>
<li>对象不用自己组装，拿来就用。</li>
<li>享受单例的好处，效率高，不浪费空间。</li>
<li>统一配置，便于修改。</li>
<li>便于单元测试，方便切换mock组件。</li>
<li>便于进行AOP操作，对于使用者是透明的。</li>
</ol>
<p>缺点：对象生成因为是使用反射编程，在效率上有损耗</p>
<h2 id="IOC容器的原理"><a href="#IOC容器的原理" class="headerlink" title="IOC容器的原理"></a>IOC容器的原理</h2><p>IOC容器是个Bean工厂，管理我们所有的对象以及依赖关系。</p>
<ul>
<li>通过Java的反射技术获取类的所有信息(成员变量、类名等)</li>
<li>通过配置文件(xml)或者注解来描述类与类之间的关系</li>
<li>最后构建出对应的对象和依赖关系</li>
</ul>
<p>Spring IOC容器实现对象的创建和依赖的步骤：</p>
<ol>
<li>根据Bean配置信息在容器内部创建Bean定义注册表</li>
<li>根据注册表加载、实例化bean、建立Bean与Bean之间的依赖关系</li>
<li>将这些准备就绪的Bean放到Map缓存池中，等待应用程序调用，getBean获得实例</li>
</ol>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/143513514351351345135.png"></p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503174653.png"></p>
<p>IOC容器装配Bean方法</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/Snipaste_2021-05-03_17-21-21.png"></p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503172157.png"></p>
<p>IOC容器是工厂模式的一个实现，提供了控制反转功能，<strong>用来把应用的配置和依赖从真正的应用代码中分离</strong>。可简单分成两种：</p>
<ul>
<li><p>BeanFactory</p>
</li>
<li><ul>
<li>这是最基础、面向Spring的</li>
</ul>
</li>
<li><p>ApplicationContext</p>
</li>
<li><ul>
<li>建立在 BeanFactory 基础之上，提供抽象的面向应用的服务。<ul>
<li>MessageSource，提供国际化的消息服务，扩展类 MessageResource 接口</li>
<li>资源访问，如 URL 和文件</li>
<li>事件传播</li>
<li>载入多个（有继承关系）上下文，使得每一个上下文都专注于一个特定的层次，比如应用的 web 层。</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>区别：</p>
<ul>
<li><p>BeanFactory：是延迟加载，如果 Bean 的某一个属性没有注入，BeanFactory 加载后，直至第一次使用调用 getBean 方法才会抛出异常</p>
</li>
<li><p>ApplicationContext：则在初始化自身时检验，这样有利于检查所依赖属性是否注入；所以通常情况下使用 ApplicationContext。</p>
</li>
</ul>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210503174013.png"></p>
<p>参考：</p>
<p>Spring入门这一篇就够了：<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/gGzgLaiTzpGemPw_h_r-Hw">https://mp.weixin.qq.com/s/gGzgLaiTzpGemPw_h_r-Hw</a></p>
<p>Spring【依赖注入】就是这么简单：<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/7eAnJw10VHdt6N5ZteTMhg">https://mp.weixin.qq.com/s/7eAnJw10VHdt6N5ZteTMhg</a></p>
<p>Spring IOC知识点一网打尽：<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/TYftOpe6J1Y4EDNDxKKqXg">https://mp.weixin.qq.com/s/TYftOpe6J1Y4EDNDxKKqXg</a></p>
<p>Spring思维导图（ioc篇）：<a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000017348726">https://segmentfault.com/a/1190000017348726</a></p>
<p>Spring的IOC，属性注入，bean的理解：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_41737632/article/details/109615913">https://blog.csdn.net/weixin_41737632/article/details/109615913</a></p>
<p>SpringIOC和AOP原理 设计模式：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/40d0a6ad21d2">https://www.jianshu.com/p/40d0a6ad21d2</a></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/05/03/Java/Spring%20IOC%E5%8E%9F%E7%90%86/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Spring/" rel="tag">Spring</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/05/03/Mysql/Mysql%E9%94%81/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Mysql锁
          
        </div>
      </a>
    
    
      <a href="/2021/04/14/network/%E4%B8%BA%E4%BB%80%E4%B9%88tcp%E5%BB%BA%E7%AB%8B%E8%BF%9E%E6%8E%A5%E9%9C%80%E8%A6%81%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B%EF%BC%8C%E6%96%AD%E5%BC%80%E8%BF%9E%E6%8E%A5%E9%9C%80%E8%A6%81%E5%9B%9B%E6%AC%A1%E6%8C%A5%E6%89%8B/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">为什么tcp建立连接需要三次握手，断开连接需要四次挥手</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021
        <i class="ri-heart-fill heart_icon"></i> Chenyy
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="ChenyyのBlog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">文章</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/book">收藏</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about/me">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>